home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / clang / gnumake.zip / REMOTE-C.C < prev    next >
C/C++ Source or Header  |  1994-03-23  |  8KB  |  271 lines

  1. /* GNU Make remote job exportation interface to the Customs daemon.
  2.    THIS CODE IS NOT SUPPORTED BY THE GNU PROJECT.
  3.    Please do not send bug reports or questions about it to
  4.    the Make maintainers.
  5.  
  6. Copyright (C) 1988, 1989, 1992, 1993 Free Software Foundation, Inc.
  7. This file is part of GNU Make.
  8.  
  9. GNU Make is free software; you can redistribute it and/or modify
  10. it under the terms of the GNU General Public License as published by
  11. the Free Software Foundation; either version 2, or (at your option)
  12. any later version.
  13.  
  14. GNU Make is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. GNU General Public License for more details.
  18.  
  19. You should have received a copy of the GNU General Public License
  20. along with GNU Make; see the file COPYING.  If not, write to
  21. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  22.  
  23. #include "make.h"
  24. #include "commands.h"
  25. #include "job.h"
  26. #include <sys/time.h>
  27. #include <netdb.h>
  28.  
  29. #define __STRICT_BSD__        /* Don't make conflicting declarations.  */
  30. #include "customs.h"
  31.  
  32.  
  33. char *remote_description = "Customs";
  34.  
  35. /* File name of the Customs `export' client command.
  36.    A full path name can be used to avoid some path-searching overhead.  */
  37. #define    EXPORT_COMMAND    "/usr/local/bin/export"
  38.  
  39. /* ExportPermit gotten by start_remote_job_p, and used by start_remote_job.  */
  40. static ExportPermit permit;
  41.  
  42. /* Normalized path name of the current directory.  */
  43. static char *normalized_cwd;
  44.  
  45. /* Return nonzero if the next job should be done remotely.  */
  46.  
  47. int
  48. start_remote_job_p ()
  49. {
  50.   static int inited = 0;
  51.   int status;
  52.  
  53.   /* Allow the user to turn off job exportation
  54.      (useful while he is debugging Customs, for example).  */
  55.   if (getenv ("GNU_MAKE_NO_CUSTOMS") != 0)
  56.     return 0;
  57.  
  58.   if (!inited)
  59.     {
  60.       /* For secure Customs, make is installed setuid root and
  61.      Customs requires a privileged source port be used.  */
  62.       make_access ();
  63.  
  64.       /* Ping the daemon once to see if it is there.  */
  65.       inited = Customs_Ping () == RPC_SUCCESS ? 1 : -1;
  66.  
  67.       /* Return to normal user access.  */
  68.       user_access ();
  69.  
  70.       if (starting_directory == 0)
  71.     /* main couldn't figure it out.  */
  72.     inited = -1;
  73.       else
  74.     {
  75.       /* Normalize the current directory path name to something
  76.          that should work on all machines exported to.  */
  77.  
  78.       normalized_cwd = (char *) xmalloc (GET_PATH_MAX);
  79.       strcpy (normalized_cwd, starting_directory);
  80.       if (Customs_NormPath (normalized_cwd, GET_PATH_MAX) < 0)
  81.         /* Path normalization failure means using Customs
  82.            won't work, but it's not really an error.  */
  83.         inited = -1;
  84.     }
  85.     }
  86.  
  87.   if (inited < 0)
  88.     return 0;
  89.  
  90.   status = Customs_Host (EXPORT_SAME, &permit);
  91.   if (status != RPC_SUCCESS)
  92.     {
  93.       if (debug_flag)
  94.     printf ("Customs won't export: %s\n", Rpc_ErrorMessage (status));
  95.       return 0;
  96.     }
  97.  
  98.   return !CUSTOMS_FAIL (&permit.addr);
  99. }
  100.  
  101. /* Start a remote job running the command in ARGV, with environment from
  102.    ENVP.  It gets standard input from STDIN_FD.  On failure, return
  103.    nonzero.  On success, return zero, and set *USED_STDIN to nonzero if it
  104.    will actually use STDIN_FD, zero if not, set *ID_PTR to a unique
  105.    identification, and set *IS_REMOTE to nonzero if the job is remote, zero
  106.    if it is local (meaning *ID_PTR is a process ID).  */
  107.  
  108. int
  109. start_remote_job (argv, envp, stdin_fd, is_remote, id_ptr, used_stdin)
  110.      char **argv, **envp;
  111.      int stdin_fd;
  112.      int *is_remote;
  113.      int *id_ptr;
  114.      int *used_stdin;
  115. {
  116.   extern int vfork (), execve ();
  117.   char waybill[MAX_DATA_SIZE], msg[128];
  118.   struct timeval timeout;
  119.   struct sockaddr_in sin;
  120.   int len;
  121.   int retsock, retport, sock;
  122.   Rpc_Stat status;
  123.   int pid;
  124.  
  125.   /* Create the return socket.  */
  126.   retsock = Rpc_UdpCreate (True, 0);
  127.   if (retsock < 0)
  128.     {
  129.       error ("exporting: Couldn't create return socket.");
  130.       return 1;
  131.     }
  132.  
  133.   /* Get the return socket's port number.  */
  134.   len = sizeof (sin);
  135.   if (getsockname (retsock, (struct sockaddr *) &sin, &len) < 0)
  136.     {
  137.       (void) close (retsock);
  138.       perror_with_name ("exporting: ", "getsockname");
  139.       return 1;
  140.     }
  141.   retport = sin.sin_port;
  142.  
  143.   /* Create the TCP socket for talking to the remote child.  */
  144.   sock = Rpc_TcpCreate (False, 0);
  145.  
  146.   /* Create a WayBill to give to the server.  */
  147.   len = Customs_MakeWayBill (&permit, normalized_cwd, argv[0], argv,
  148.                  envp, retport, waybill);
  149.  
  150.   /* Modify the waybill as if the remote child had done `child_access ()'.  */
  151.   {
  152.     WayBill *wb = (WayBill *) waybill;
  153.     wb->euid = wb->ruid;
  154.     wb->rgid = wb->rgid;
  155.   }
  156.  
  157.   /* Send the request to the server, timing out in 20 seconds.  */
  158.   timeout.tv_usec = 0;
  159.   timeout.tv_sec = 20;
  160.   sin.sin_family = AF_INET;
  161.   sin.sin_port = htons (Customs_Port ());
  162.   sin.sin_addr = permit.addr;
  163.   status = Rpc_Call (sock, &sin, (Rpc_Proc) CUSTOMS_IMPORT,
  164.              len, (Rpc_Opaque) waybill,
  165.              sizeof(msg), (Rpc_Opaque) msg,
  166.              1, &timeout);
  167.   if (status != RPC_SUCCESS)
  168.     {
  169.       (void) close (retsock);
  170.       (void) close (sock);
  171.       error ("exporting: %s", Rpc_ErrorMessage (status));
  172.       return 1;
  173.     }
  174.   else if (msg[0] != 'O' || msg[1] != 'k' || msg[2] != '\0')
  175.     {
  176.       (void) close (retsock);
  177.       (void) close (sock);
  178.       error ("CUSTOMS_IMPORT: %s", msg);
  179.       return 1;
  180.     }
  181.   else if (debug_flag)
  182.     {
  183.       struct hostent *host = gethostbyaddr (&permit.addr, sizeof (permit.addr),
  184.                         AF_INET);
  185.       printf ("Job exported to %s ID %u\n",
  186.           host == 0 ? inet_ntoa (permit.addr) : host->h_name,
  187.           permit.id);
  188.     }
  189.  
  190.   fflush (stdout);
  191.   fflush (stderr);
  192.  
  193.   pid = vfork ();
  194.   if (pid < 0)
  195.     {
  196.       /* The fork failed!  */
  197.       perror_with_name ("vfork", "");
  198.       return 1;
  199.     }
  200.   else if (pid == 0)
  201.     {
  202.       /* Child side.  Run `export' to handle the connection.  */
  203.       static char sock_buf[20], retsock_buf[20], id_buf[20];
  204.       static char *new_argv[6] =
  205.     { EXPORT_COMMAND, "-id", sock_buf, retsock_buf, id_buf, 0 };
  206.  
  207.       /* Set up the arguments.  */
  208.       (void) sprintf (sock_buf, "%d", sock);
  209.       (void) sprintf (retsock_buf, "%d", retsock);
  210.       (void) sprintf (id_buf, "%x", permit.id);
  211.  
  212.       /* Get the right stdin.  */
  213.       if (stdin_fd != 0)
  214.     (void) dup2 (stdin_fd, 0);
  215.  
  216.       /* Unblock signals in the child.  */
  217.       unblock_sigs ();
  218.  
  219.       /* Run the command.  */
  220.       exec_command (new_argv, envp);
  221.     }
  222.  
  223.   /* Parent side.  Return the `export' process's ID.  */
  224.   (void) close (retsock);
  225.   (void) close (sock);
  226.   *is_remote = 0;
  227.   *id_ptr = pid;
  228.   return 0;
  229. }
  230.  
  231. /* Get the status of a dead remote child.  Block waiting for one to die
  232.    if BLOCK is nonzero.  Set *EXIT_CODE_PTR to the exit status, *SIGNAL_PTR
  233.    to the termination signal or zero if it exited normally, and *COREDUMP_PTR
  234.    nonzero if it dumped core.  Return the ID of the child that died,
  235.    0 if we would have to block and !BLOCK, or < 0 if there were none.  */
  236.  
  237. int
  238. remote_status (exit_code_ptr, signal_ptr, coredump_ptr, block)
  239.      int *exit_code_ptr, *signal_ptr, *coredump_ptr;
  240.      int block;
  241. {
  242.   return -1;
  243. }
  244.  
  245. /* Block asynchronous notification of remote child death.
  246.    If this notification is done by raising the child termination
  247.    signal, do not block that signal.  */
  248. void
  249. block_remote_children ()
  250. {
  251.   return;
  252. }
  253.  
  254. /* Restore asynchronous notification of remote child death.
  255.    If this is done by raising the child termination signal,
  256.    do not unblock that signal.  */
  257. void
  258. unblock_remote_children ()
  259. {
  260.   return;
  261. }
  262.  
  263. /* Send signal SIG to child ID.  Return 0 if successful, -1 if not.  */
  264. int
  265. remote_kill (id, sig)
  266.      int id;
  267.      int sig;
  268. {
  269.   return -1;
  270. }
  271.